<!DOCTYPE html>
<html lang="zh-cn">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <title>我来学Kotlin-面向对象之类和继承 - (power up)</title>
  <meta name="renderer" content="webkit" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>

<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />

<meta name="theme-color" content="#f8f5ec" />
<meta name="msapplication-navbutton-color" content="#f8f5ec">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="#f8f5ec">


<meta name="author" content="Kevin Jiang" /><meta name="description" content="使用关键字class来定义类： 1 2 class Invoice { } 类的定义由header（类名，主构造函数，参数及类型等）和header构成，主体使用花括号包裹。头" /><meta name="keywords" content="KevinJiang, AI大模型, AI落地, 全栈工程师, Java, Spring Boot" />






<meta name="generator" content="Hugo 0.84.4 with theme even" />


<link rel="canonical" href="http://kevinjiang.info/post/kotlin/%E6%88%91%E6%9D%A5%E5%AD%A6kotlin-%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1%E4%B9%8B%E7%B1%BB%E5%92%8C%E7%BB%A7%E6%89%BF/" />
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
<link rel="manifest" href="/manifest.json">
<link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5">



<link href="/sass/main.min.78f8f17bab244b9ee62ad16480c9584d5fc2db06ae20681d1ca225cefd80767c.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.1.20/dist/jquery.fancybox.min.css" integrity="sha256-7TyXnr2YU040zfSP+rEcz29ggW4j56/ujTPwjMzyqFY=" crossorigin="anonymous">


<meta property="og:title" content="我来学Kotlin-面向对象之类和继承" />
<meta property="og:description" content="使用关键字class来定义类： 1 2 class Invoice { } 类的定义由header（类名，主构造函数，参数及类型等）和header构成，主体使用花括号包裹。头" />
<meta property="og:type" content="article" />
<meta property="og:url" content="http://kevinjiang.info/post/kotlin/%E6%88%91%E6%9D%A5%E5%AD%A6kotlin-%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1%E4%B9%8B%E7%B1%BB%E5%92%8C%E7%BB%A7%E6%89%BF/" /><meta property="article:section" content="post" />
<meta property="article:published_time" content="2017-06-16T14:58:07+00:00" />
<meta property="article:modified_time" content="2017-06-16T14:58:07+00:00" />

<meta itemprop="name" content="我来学Kotlin-面向对象之类和继承">
<meta itemprop="description" content="使用关键字class来定义类： 1 2 class Invoice { } 类的定义由header（类名，主构造函数，参数及类型等）和header构成，主体使用花括号包裹。头"><meta itemprop="datePublished" content="2017-06-16T14:58:07+00:00" />
<meta itemprop="dateModified" content="2017-06-16T14:58:07+00:00" />
<meta itemprop="wordCount" content="2802">
<meta itemprop="keywords" content="Kotlin,Kotlin教程," /><meta name="twitter:card" content="summary"/>
<meta name="twitter:title" content="我来学Kotlin-面向对象之类和继承"/>
<meta name="twitter:description" content="使用关键字class来定义类： 1 2 class Invoice { } 类的定义由header（类名，主构造函数，参数及类型等）和header构成，主体使用花括号包裹。头"/>

<!--[if lte IE 9]>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/classlist/1.1.20170427/classList.min.js"></script>
<![endif]-->

<!--[if lt IE 9]>
  <script src="https://cdn.jsdelivr.net/npm/html5shiv@3.7.3/dist/html5shiv.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/respond.js@1.4.2/dest/respond.min.js"></script>
<![endif]-->

</head>
<body>
  <div id="mobile-navbar" class="mobile-navbar">
  <div class="mobile-header-logo">
    <a href="/" class="logo">(Power up)</a>
  </div>
  <div class="mobile-navbar-icon">
    <span></span>
    <span></span>
    <span></span>
  </div>
</div>
<nav id="mobile-menu" class="mobile-menu slideout-menu">
  <ul class="mobile-menu-list">
    <a href="/">
        <li class="mobile-menu-item">首页</li>
      </a><a href="/post/">
        <li class="mobile-menu-item">归档</li>
      </a><a href="/tags/">
        <li class="mobile-menu-item">标签</li>
      </a><a href="/categories/">
        <li class="mobile-menu-item">分类</li>
      </a><a href="/resume/">
        <li class="mobile-menu-item">简历</li>
      </a>
  </ul>

  


</nav>

  <div class="container" id="mobile-panel">
    <header id="header" class="header">
        <div class="logo-wrapper">
  <a href="/" class="logo">(Power up)</a>
</div>





<nav class="site-navbar">
  <ul id="menu" class="menu">
    <li class="menu-item">
        <a class="menu-item-link" href="/">首页</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/post/">归档</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/tags/">标签</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/categories/">分类</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/resume/">简历</a>
      </li>
  </ul>
</nav>

    </header>

    <main id="main" class="main">
      <div class="content-wrapper">
        <div id="content" class="content">
          <article class="post">
    
    <header class="post-header">
      <h1 class="post-title">我来学Kotlin-面向对象之类和继承</h1>

      <div class="post-meta">
        <span class="post-time"> 2017-06-16 </span>
        <div class="post-category">
            <a href="/categories/%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80/"> 编程语言 </a>
            </div>
          <span class="more-meta"> 约 2802 字 </span>
          <span class="more-meta"> 预计阅读 6 分钟 </span>
        
      </div>
    </header>

    <div class="post-toc" id="post-toc">
  <h2 class="post-toc-title">文章目录</h2>
  <div class="post-toc-content always-active">
    <nav id="TableOfContents">
  <ul>
    <li><a href="#构造函数">构造函数</a></li>
    <li><a href="#辅助构造函数">辅助构造函数</a></li>
    <li><a href="#创建实例">创建实例</a></li>
    <li><a href="#类成员">类成员</a></li>
    <li><a href="#继承">继承</a></li>
    <li><a href="#方法覆写overriding-methods">方法覆写（Overriding Methods）</a></li>
    <li><a href="#属性覆写overriding-properties">属性覆写（Overriding Properties）</a></li>
    <li><a href="#覆写规则">覆写规则</a></li>
    <li><a href="#抽象类">抽象类</a></li>
    <li><a href="#伴生对象在scala是这么称呼">伴生对象（在Scala是这么称呼）</a></li>
  </ul>
</nav>
  </div>
</div>
    <div class="post-content">
      <p>使用关键字<code>class</code>来定义类：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-Kotlin" data-lang="Kotlin"><span class="k">class</span> <span class="nc">Invoice</span> <span class="p">{</span>
<span class="p">}</span>
</code></pre></td></tr></table>
</div>
</div><p>类的定义由header（类名，主构造函数，参数及类型等）和header构成，主体使用花括号包裹。头和主体都不是必须的，如果没有主体花括号可以省略。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-Kotlin" data-lang="Kotlin"><span class="k">class</span> <span class="nc">Empty</span>
</code></pre></td></tr></table>
</div>
</div><!-- raw HTML omitted -->
<h2 id="构造函数">构造函数</h2>
<p>类有一个主构造函数以及一个或多个辅构造函数。主构造函数是header的一部分，紧跟在类名的后面。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-Kotlin" data-lang="Kotlin"><span class="k">class</span> <span class="nc">Person</span> <span class="k">constructor</span><span class="p">(</span><span class="n">firstName</span><span class="p">:</span> <span class="n">String</span><span class="p">)</span> <span class="p">{</span>
<span class="p">}</span>
</code></pre></td></tr></table>
</div>
</div><p>如果主构造函数没有注解或者可见性修饰符，关键字<code>constructor</code>可以省略：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-Kotlin" data-lang="Kotlin"><span class="k">class</span> <span class="nc">Person</span><span class="p">(</span><span class="n">firstName</span><span class="p">:</span> <span class="n">String</span><span class="p">)</span> <span class="p">{</span>
<span class="p">}</span>
</code></pre></td></tr></table>
</div>
</div><p>主构造函数不能包含任何的求值表达式，仅仅是定义参数及类型。如果要做一些初始化的工作，可以放在以<code>init</code>关键字开头的代码块当中：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-Kotlin" data-lang="Kotlin"><span class="k">class</span> <span class="nc">Customer</span><span class="p">(</span><span class="n">name</span><span class="p">:</span> <span class="n">String</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">init</span> <span class="p">{</span>
        <span class="n">logger</span><span class="p">.</span><span class="n">info</span><span class="p">(</span><span class="s2">&#34;Customer initialized with value </span><span class="si">${name}</span><span class="s2">&#34;</span><span class="p">)</span>
    <span class="p">}</span>
<span class="p">}</span>
</code></pre></td></tr></table>
</div>
</div><p>主构造函数的参数可以在初始化代码块中使用。同样也可以在属性的定义及初始化的时候使用：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-Kotlin" data-lang="Kotlin"><span class="k">class</span> <span class="nc">Customer</span><span class="p">(</span><span class="n">name</span><span class="p">:</span> <span class="n">String</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">val</span> <span class="py">customerKey</span> <span class="p">=</span> <span class="n">name</span><span class="p">.</span><span class="n">toUpperCase</span><span class="p">()</span>
<span class="p">}</span>
</code></pre></td></tr></table>
</div>
</div><p>属性的定义及初始化可以简单的通过构造函数完成，同时可以定义属性的可变性（mutable/immutable），<code>var</code>是可变，<code>val</code>是不可变：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-Kotlin" data-lang="Kotlin"><span class="k">class</span> <span class="nc">Person</span><span class="p">(</span><span class="k">val</span> <span class="py">firstName</span><span class="p">:</span> <span class="n">String</span><span class="p">,</span> <span class="k">val</span> <span class="py">lastName</span><span class="p">:</span> <span class="n">String</span><span class="p">,</span> <span class="k">var</span> <span class="py">age</span><span class="p">:</span> <span class="n">Int</span><span class="p">)</span> <span class="p">{</span>
    <span class="c1">// ...
</span><span class="c1"></span><span class="p">}</span>
</code></pre></td></tr></table>
</div>
</div><p>如果有可见性及注释修饰符，那么<code>constructor</code>关键字是不可以省略的，而且要请在<code>constructor</code>前面：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-Kotlin" data-lang="Kotlin"><span class="k">class</span> <span class="nc">Customer</span> <span class="k">public</span> <span class="nd">@Inject</span> <span class="k">constructor</span><span class="p">(</span><span class="n">name</span><span class="p">:</span> <span class="n">String</span><span class="p">)</span> <span class="p">{</span> <span class="o">..</span><span class="p">.</span> <span class="p">}</span>
</code></pre></td></tr></table>
</div>
</div><h2 id="辅助构造函数">辅助构造函数</h2>
<p>辅助构造函数就像Java的多构造函数类似。在Kotlin使用<code>constructorr</code>关键字定义，在Java中是使用与类同名的函数名定义的。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-Kotlin" data-lang="Kotlin"><span class="k">class</span> <span class="nc">Person</span> <span class="p">{</span>
    <span class="k">constructor</span><span class="p">(</span><span class="n">parent</span><span class="p">:</span> <span class="n">Person</span><span class="p">)</span> <span class="p">{</span>
        <span class="n">parent</span><span class="p">.</span><span class="n">children</span><span class="p">.</span><span class="n">add</span><span class="p">(</span><span class="k">this</span><span class="p">)</span>
    <span class="p">}</span>
<span class="p">}</span>
</code></pre></td></tr></table>
</div>
</div><p>如果类有辅助构造函数同时还有主构造函数，那么辅助构造函数必须要显示的调用主构造函数，直接调用，或者调用其他的辅助构造函数。调用其他的构造函数可以使用<code>this</code>关键字：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-Kotlin" data-lang="Kotlin"><span class="k">class</span> <span class="nc">Person</span><span class="p">(</span><span class="k">val</span> <span class="py">name</span><span class="p">:</span> <span class="n">String</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">constructor</span><span class="p">(</span><span class="n">name</span><span class="p">:</span> <span class="n">String</span><span class="p">,</span> <span class="n">parent</span><span class="p">:</span> <span class="n">Person</span><span class="p">)</span> <span class="p">:</span> <span class="k">this</span><span class="p">(</span><span class="n">name</span><span class="p">)</span> <span class="p">{</span>
        <span class="n">parent</span><span class="p">.</span><span class="n">children</span><span class="p">.</span><span class="n">add</span><span class="p">(</span><span class="k">this</span><span class="p">)</span>
    <span class="p">}</span>
<span class="p">}</span>
</code></pre></td></tr></table>
</div>
</div><p>如果类没有定义任何的构造函数（主或者辅助构造函数），默认会有一个无参数的主构造函数，且其可见性是<code>public</code>的。如果要修改可见性，必须显示的定义一个空的构造函数：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-Kotlin" data-lang="Kotlin"><span class="k">class</span> <span class="nc">DontCreateMe</span> <span class="k">private</span> <span class="k">constructor</span> <span class="p">()</span> <span class="p">{</span> <span class="p">}</span>
</code></pre></td></tr></table>
</div>
</div><p>总结一下辅助构造函数，一句话：不管是直接调用主构造函数，可以调用其他的构造函数，一个辅助构造函数最终必须调用到主构造函数。</p>
<ul>
<li>辅助构造函数 &ndash;&gt; 主构造函数</li>
<li>辅助构造函数 &ndash;&gt; 其他的辅助构造函数 &ndash;&gt; 主构造函数</li>
</ul>
<blockquote>
<p>注：
在JVM平台（Kotlin可以编译到js或者native），如果主构造函数的所有参数都有默认值，编译器会生成另外一个无参数的构造函数，无参数构造函数会调用主构造函数。这使得可以不用传递参数来生成相应的对象
<code>class Customer(val customerName: String = &quot;&quot;)</code>
可以直接调用<code>Customer()</code>来实例化一个对象</p>
</blockquote>
<h2 id="创建实例">创建实例</h2>
<p>创建实例非常简单，就像调用一个函数一样。不像Java需要<code>new</code>关键字。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-Kotlin" data-lang="Kotlin"><span class="k">class</span> <span class="nc">Invoice</span>
<span class="k">class</span> <span class="nc">Customer</span><span class="p">(</span><span class="k">val</span> <span class="py">customerName</span><span class="p">:</span> <span class="n">String</span> <span class="p">=</span> <span class="s2">&#34;&#34;</span><span class="p">)</span>
<span class="k">val</span> <span class="py">invoice</span> <span class="p">=</span> <span class="n">Invoice</span><span class="p">()</span>
<span class="k">val</span> <span class="py">customer</span> <span class="p">=</span> <span class="n">Customer</span><span class="p">(</span><span class="s2">&#34;Kevin Jiang&#34;</span><span class="p">)</span>
<span class="k">val</span> <span class="py">customer</span> <span class="p">=</span> <span class="n">Customer</span><span class="p">()</span>
</code></pre></td></tr></table>
</div>
</div><h2 id="类成员">类成员</h2>
<p>类可以包含：</p>
<ul>
<li>构造函数和初始化代码块</li>
<li>方法</li>
<li>属性</li>
<li>嵌套类，内部类</li>
<li>单例对象</li>
</ul>
<h2 id="继承">继承</h2>
<p>所有的类都有一个共同的父类<code>Any</code>，这是没有指定父类的默认父类：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-Kotlin" data-lang="Kotlin"><span class="k">class</span> <span class="nc">Example</span> <span class="c1">// 默认继承自`Any`
</span></code></pre></td></tr></table>
</div>
</div><p><code>Any</code>不是Java的<code>java.lang.Object</code>，<code>Any</code>只包含<code>equals()</code>, <code>hashCode()</code>, <code>toString()</code>三个方法。可以通过把父类放在冒号后面来指定父类，而且必须显示的指定调用父类的哪个构造函数，如果父类是无参娄的构造，也需要显示的写明。而且父类必须明确标记为<code>open</code>，不然就是<code>final</code>不能被继承：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-Kotlin" data-lang="Kotlin"><span class="k">open</span> <span class="k">class</span> <span class="nc">Base</span><span class="p">(</span><span class="n">p</span><span class="p">:</span> <span class="n">Int</span><span class="p">)</span>
<span class="k">class</span> <span class="nc">Derived</span><span class="p">(</span><span class="n">p</span><span class="p">:</span> <span class="n">Int</span><span class="p">)</span> <span class="p">:</span> <span class="n">Base</span><span class="p">(</span><span class="n">p</span><span class="p">)</span>
</code></pre></td></tr></table>
</div>
</div><p>如果类没有主构造函数，每个辅助构造函数必须使用<code>super</code>关键字调用父类的构造函数，或者调用自己其他的构造函数。在下面的例子中，不同的构造函数可以调用父类不同的构造函数：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-Kotlin" data-lang="Kotlin"><span class="k">class</span> <span class="nc">MyView</span> <span class="p">:</span> <span class="n">View</span> <span class="p">{</span>
    <span class="k">constructor</span><span class="p">(</span><span class="n">ctx</span><span class="p">:</span> <span class="n">Context</span><span class="p">)</span> <span class="p">:</span> <span class="k">super</span><span class="p">(</span><span class="n">ctx</span><span class="p">)</span>
    <span class="k">constructor</span><span class="p">(</span><span class="n">ctx</span><span class="p">:</span> <span class="n">Context</span><span class="p">,</span> <span class="n">attrs</span><span class="p">:</span> <span class="n">AttributeSet</span><span class="p">)</span> <span class="p">:</span> <span class="k">super</span><span class="p">(</span><span class="n">ctx</span><span class="p">,</span> <span class="n">attrs</span><span class="p">)</span>
<span class="p">}</span>
</code></pre></td></tr></table>
</div>
</div><p><strong>在Kotlin里，类默认是<code>final</code>的&ndash;也就是不能被继承的，必须使用<code>open</code>关键字标记才可以被继承</strong></p>
<p>总结一下继承当中的的构造函数定义，两句话：子类的主构造函数要调用父类的主构造函数；子类的辅助构造函数必须最终调用到父类的主构造函数：</p>
<ul>
<li><code>class Sub(a: Int): Parent(a)</code>子类的主构造函数调用父类的主构造函数</li>
<li>子类有主构造函数时候，子类的辅助构造函数调用自己本身的主构造函数或者自己本身的辅助构造函数，这个情况跟上面辅助构造函数的总结一样，最后调用到了子类的请构造函数，最终还是调用到了父类的主构造函数</li>
<li>子类没有主构造函数，那么子类的辅助构造函数就需要使用<code>super</code>关键字调用父类的构造函数（主构造函数或者辅助构造函数），最后也是调用到了父类的主构造函数</li>
</ul>
<h2 id="方法覆写overriding-methods">方法覆写（Overriding Methods）</h2>
<p>在Kotlin里，覆写父类的方法需要显示的指明，使用<code>override</code>关键字可以覆写父类的方法，而且父类的方法必须标注为<code>open</code>才可以被子类覆写：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-Kotlin" data-lang="Kotlin"><span class="k">open</span> <span class="k">class</span> <span class="nc">Base</span> <span class="p">{</span>
    <span class="k">open</span> <span class="k">fun</span> <span class="nf">v</span><span class="p">()</span> <span class="p">{}</span>
    <span class="k">fun</span> <span class="nf">nv</span><span class="p">()</span> <span class="p">{}</span>
<span class="p">}</span>
<span class="k">class</span> <span class="nc">Derived</span><span class="p">()</span> <span class="p">:</span> <span class="n">Base</span><span class="p">()</span> <span class="p">{</span>
    <span class="k">override</span> <span class="k">fun</span> <span class="nf">v</span><span class="p">()</span> <span class="p">{}</span>
<span class="p">}</span>
</code></pre></td></tr></table>
</div>
</div><p>使用了<code>override</code>关键字的方法，默认是<code>open</code>的，可以被其子类覆写，如果不想让子类覆写，需要在前面使用<code>final</code>关键字指定：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-Kotlin" data-lang="Kotlin"><span class="k">open</span> <span class="k">class</span> <span class="nc">AnotherDerived</span><span class="p">()</span> <span class="p">:</span> <span class="n">Base</span><span class="p">()</span> <span class="p">{</span>
    <span class="k">final</span> <span class="k">override</span> <span class="k">fun</span> <span class="nf">v</span><span class="p">()</span> <span class="p">{}</span>
<span class="p">}</span>
</code></pre></td></tr></table>
</div>
</div><h2 id="属性覆写overriding-properties">属性覆写（Overriding Properties）</h2>
<p>属性的覆写跟方法的覆写差不多，<code>opeen</code>和<code>override</code>是必不可少的：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-Kotlin" data-lang="Kotlin"><span class="k">open</span> <span class="k">class</span> <span class="nc">Foo</span> <span class="p">{</span>
    <span class="k">open</span> <span class="k">val</span> <span class="py">x</span><span class="p">:</span> <span class="n">Int</span> <span class="k">get</span> <span class="p">{</span> <span class="o">..</span><span class="p">.</span> <span class="p">}</span>
<span class="p">}</span>
<span class="k">class</span> <span class="nc">Bar1</span> <span class="p">:</span> <span class="n">Foo</span><span class="p">()</span> <span class="p">{</span>
    <span class="k">override</span> <span class="k">val</span> <span class="py">x</span><span class="p">:</span> <span class="n">Int</span> <span class="p">=</span> <span class="o">..</span><span class="p">.</span>
<span class="p">}</span>
</code></pre></td></tr></table>
</div>
</div><p>使用<code>val</code>定义的属性，可以被覆写为<code>var</code>，反过来是不行的。</p>
<p>可以在主构造函数对属性进行覆写，像下面的例子，在Foo接口中定义了一个<code>count</code>属性，Bar类在主构造函数使用<code>override</code>关键字对Foo接口中的count进行了覆写：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span><span class="lnt">8
</span><span class="lnt">9
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-Kotlin" data-lang="Kotlin"><span class="k">interface</span> <span class="nc">Foo</span> <span class="p">{</span>
    <span class="k">val</span> <span class="py">count</span><span class="p">:</span> <span class="n">Int</span>
<span class="p">}</span>

<span class="k">class</span> <span class="nc">Bar1</span><span class="p">(</span><span class="k">override</span> <span class="k">val</span> <span class="py">count</span><span class="p">:</span> <span class="n">Int</span><span class="p">)</span> <span class="p">:</span> <span class="n">Foo</span>

<span class="k">class</span> <span class="nc">Bar2</span> <span class="p">:</span> <span class="n">Foo</span> <span class="p">{</span>
    <span class="k">override</span> <span class="k">var</span> <span class="py">count</span><span class="p">:</span> <span class="n">Int</span> <span class="p">=</span> <span class="m">0</span>
<span class="p">}</span>
</code></pre></td></tr></table>
</div>
</div><h2 id="覆写规则">覆写规则</h2>
<p>Kotlin和Java一样，只允许继承一个类，可以继承多个接口。当一个类有多个继承（多个接口，一个类+一个或多个接口）时，如果出现了同名的方法，那么子类必须自己实现这个方法，像下面的例子，类<code>C</code>必须实现<code>f()</code>方法，而<code>a()</code>，<code>b()</code>可以是继承自父类和父接口。调用父类或父接口的语法使用<code>super</code>关键字，使用<code>&lt;&gt;</code>括起来父类或父接口</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-Kotlin" data-lang="Kotlin"><span class="k">open</span> <span class="k">class</span> <span class="nc">A</span> <span class="p">{</span>
    <span class="k">open</span> <span class="k">fun</span> <span class="nf">f</span><span class="p">()</span> <span class="p">{</span> <span class="n">print</span><span class="p">(</span><span class="s2">&#34;A&#34;</span><span class="p">)</span> <span class="p">}</span>
    <span class="k">fun</span> <span class="nf">a</span><span class="p">()</span> <span class="p">{</span> <span class="n">print</span><span class="p">(</span><span class="s2">&#34;a&#34;</span><span class="p">)</span> <span class="p">}</span>
<span class="p">}</span>

<span class="k">interface</span> <span class="nc">B</span> <span class="p">{</span>
    <span class="k">fun</span> <span class="nf">f</span><span class="p">()</span> <span class="p">{</span> <span class="n">print</span><span class="p">(</span><span class="s2">&#34;B&#34;</span><span class="p">)</span> <span class="p">}</span> <span class="c1">// interface members are &#39;open&#39; by default
</span><span class="c1"></span>    <span class="k">fun</span> <span class="nf">b</span><span class="p">()</span> <span class="p">{</span> <span class="n">print</span><span class="p">(</span><span class="s2">&#34;b&#34;</span><span class="p">)</span> <span class="p">}</span>
<span class="p">}</span>

<span class="k">class</span> <span class="nc">C</span><span class="p">()</span> <span class="p">:</span> <span class="n">A</span><span class="p">(),</span> <span class="n">B</span> <span class="p">{</span>
    <span class="c1">// The compiler requires f() to be overridden:
</span><span class="c1"></span>    <span class="k">override</span> <span class="k">fun</span> <span class="nf">f</span><span class="p">()</span> <span class="p">{</span>
        <span class="k">super</span><span class="p">&lt;</span><span class="n">A</span><span class="p">&gt;.</span><span class="n">f</span><span class="p">()</span> <span class="c1">// 指明调用父类A.f()
</span><span class="c1"></span>        <span class="k">super</span><span class="p">&lt;</span><span class="n">B</span><span class="p">&gt;.</span><span class="n">f</span><span class="p">()</span> <span class="c1">// 或者指明调用父接口B.f()
</span><span class="c1"></span>        <span class="c1">// 或者实现自己的逻辑
</span><span class="c1"></span>    <span class="p">}</span>
<span class="p">}</span>
</code></pre></td></tr></table>
</div>
</div><p>这一点是为了避免像C++和Python那么可以继承多个类的时候，发生菱形继承的问题，Kotlin通过上面的规则来消除菱形继承问题，这样就显得非常的明确，而不会产生歧义。</p>
<h2 id="抽象类">抽象类</h2>
<p>抽象类是含有抽象方法的类，抽象方法是没有具体实现而只有定义的一个方法，像下面抽象类<code>Derived</code>的抽象方法<code>f()</code>。如果继承了这个抽象类，必须实现这个抽象方法，或者继续用<code>abstract</code>标记这个方法和类为抽象的。这一点和Java是一样的。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-Kotlin" data-lang="Kotlin"><span class="k">open</span> <span class="k">class</span> <span class="nc">Base</span> <span class="p">{</span>
    <span class="k">open</span> <span class="k">fun</span> <span class="nf">f</span><span class="p">()</span> <span class="p">{}</span>
<span class="p">}</span>

<span class="k">abstract</span> <span class="k">class</span> <span class="nc">Derived</span> <span class="p">:</span> <span class="n">Base</span><span class="p">()</span> <span class="p">{</span>
    <span class="k">override</span> <span class="k">abstract</span> <span class="k">fun</span> <span class="nf">f</span><span class="p">()</span>
<span class="p">}</span>
</code></pre></td></tr></table>
</div>
</div><h2 id="伴生对象在scala是这么称呼">伴生对象（在Scala是这么称呼）</h2>
<p>不像Java和C#，Kotlin是没有静态方法的，Kotlin提倡定义包级别的函数（因为函数可以使用<code>fun</code>关键字直接定义在包下面）。</p>
<p>另外还可以使用伴生对象，请看伴生对象一节。</p>

    </div>

    <div class="post-copyright">
  <p class="copyright-item">
    <span class="item-title">文章作者</span>
    <span class="item-content">Kevin Jiang</span>
  </p>
  <p class="copyright-item">
    <span class="item-title">上次更新</span>
    <span class="item-content">
        2017-06-16
        
    </span>
  </p>
  
  <p class="copyright-item">
    <span class="item-title">许可协议</span>
    <span class="item-content"><a rel="license noopener" href="https://creativecommons.org/licenses/by-nc-nd/4.0/" target="_blank">CC BY-NC-ND 4.0</a></span>
  </p>
</div>
<div class="post-reward">
  <input type="checkbox" name="reward" id="reward" hidden />
  <label class="reward-button" for="reward">赞赏支持</label>
  <div class="qr-code">
    
    <label class="qr-code-image" for="reward">
        <img class="image" src="/img/wechat_pay_1242x1242.jpg">
        <span>微信打赏</span>
      </label>
    <label class="qr-code-image" for="reward">
        <img class="image" src="/img/alipay_600x600.jpg">
        <span>支付宝打赏</span>
      </label>
  </div>
</div><footer class="post-footer">
      <div class="post-tags">
          <a href="/tags/kotlin/">Kotlin</a>
          <a href="/tags/kotlin%E6%95%99%E7%A8%8B/">Kotlin教程</a>
          </div>
      <nav class="post-nav">
        <a class="prev" href="/post/kotlin/%E6%88%91%E6%9D%A5%E5%AD%A6kotlin-%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1%E4%B9%8B%E5%B1%9E%E6%80%A7%E5%92%8C%E5%AD%97%E6%AE%B5/">
            <i class="iconfont icon-left"></i>
            <span class="prev-text nav-default">我来学Kotlin-面向对象之属性和字段</span>
            <span class="prev-text nav-mobile">上一篇</span>
          </a>
        <a class="next" href="/post/kotlin/%E6%88%91%E6%9D%A5%E5%AD%A6kotlin-%E5%9F%BA%E7%A1%80%E4%B9%8B%E8%BF%94%E5%9B%9E%E5%80%BC%E4%B8%8E%E8%B7%B3%E8%BD%AC/">
            <span class="next-text nav-default">我来学Kotlin-基础之返回值与跳转</span>
            <span class="next-text nav-mobile">下一篇</span>
            <i class="iconfont icon-right"></i>
          </a>
      </nav>
    </footer>
  </article>
        </div>
        

  

  

      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="social-links">
      <a href="mailto:wenlin1988@126.com" class="iconfont icon-email" title="email"></a>
      <a href="https://www.linkedin.com/in/%E6%96%87%E6%9E%97-%E8%92%8B-0a3204126/" class="iconfont icon-linkedin" title="linkedin"></a>
      <a href="https://github.com/kevindragon" class="iconfont icon-github" title="github"></a>
  <a href="http://kevinjiang.info/index.xml" type="application/rss+xml" class="iconfont icon-rss" title="rss"></a>
</div>

<div class="copyright">
  <span class="power-by">
    由 <a class="hexo-link" href="https://gohugo.io">Hugo</a> 强力驱动
  </span>
  <span class="division">|</span>
  <span class="theme-info">
    主题 -
    <a class="theme-link" href="https://github.com/olOwOlo/hugo-theme-even">Even</a>
  </span>

  

  <span class="copyright-year">
    &copy;
    2015 -
    2023<span class="heart"><i class="iconfont icon-heart"></i></span><span>Kevin Jiang</span>
    <a href="https://beian.miit.gov.cn/" target="_blank">湘ICP备2022022745号</a>
  </span>
</div>

    </footer>

    <div class="back-to-top" id="back-to-top">
      <i class="iconfont icon-up"></i>
    </div>
  </div>
  
  <script src="https://cdn.jsdelivr.net/npm/jquery@3.2.1/dist/jquery.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/slideout@1.0.1/dist/slideout.min.js" integrity="sha256-t+zJ/g8/KXIJMjSVQdnibt4dlaDxc9zXr/9oNPeWqdg=" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.1.20/dist/jquery.fancybox.min.js" integrity="sha256-XVLffZaxoWfGUEbdzuLi7pwaUJv1cecsQJQqGLe7axY=" crossorigin="anonymous"></script>



<script type="text/javascript" src="/js/main.min.c99b103c33d1539acf3025e1913697534542c4a5aa5af0ccc20475ed2863603b.js"></script>
  <script type="text/javascript">
    window.MathJax = {
      tex: {
        inlineMath: [['$','$'], ['\\(','\\)']],
        tags: 'ams',
        }
    };
  </script>
  <script type="text/javascript" async src="/lib/mathjax/es5/tex-mml-chtml.js"></script>

<script id="baidu_analytics">
  var _hmt = _hmt || [];
  (function() {
    if (window.location.hostname === 'localhost') return;
    var hm = document.createElement("script"); hm.async = true;
    hm.src = "https://hm.baidu.com/hm.js?b73ff6d4afc4af9e582d8a5dc068bab9";
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(hm, s);
  })();
</script>






</body>
</html>
